(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initClasses();
			initTeachers(1,10000);
		});
	});
	function pagebtns(){
		checkContents("anews-contents");
		checkContents("anotices-contents");
		$("#anews-add").click(function(){
			var content = $("#anews-contents").text();
			var receivecode = $("#news-sendclass option:selected").val();
			if(checkNews()){
				sendMessage(4,receivecode,content);
			}
		});
		$("#anotices-add").click(function(){
			var content = $("#anotices-contents").text();
			var receivecode = $("#parent-cellphone option:selected").attr("cellphone");
			if(checkNotices()){
				sendMessage(5,receivecode,content);
			}
		});
		$("#anotices-send").click(function(){
			sendNotification(7);
		});
		$("#choice-student").unbind("click");
		$("#choice-student").click(function(){
			studentChoice(function(){
				studentSeachMenu();
				selectLR();
				$("#sub",parent.parent.document).unbind("click");
				$("#sub",parent.parent.document).click(function(){
					var selVal = [];
					var studentCode = [];
					var imei = [];
					var rightSel = $("#selectR",parent.parent.document);
					rightSel.find("option",parent.parent.document).each(function(){
						selVal.push(this.value);
						studentCode.push($(this).attr("studentcode"));
						imei.push($(this).attr("imei"));
					});
					selVals = selVal.join(",");
					studentCodes = studentCode.join(",");
					imeis = imei.join(",");
					$(".index-dialog-cover",parent.parent.document).hide();
					$("#student-choice-dialog",parent.parent.document).hide();
					$("#anotices-students").empty();
					$("#anotices-students").text(selVals);
					$("#anotices-students").attr("studentcodes",studentCodes);
					$("#anotices-students").attr("imeis",imeis);
					$("#anotices-students").focus();
					$("#selectR",parent.parent.document).empty();
				});
				$("#student-choice-submit",parent.parent.document).unbind("click");
				$("#student-choice-submit",parent.parent.document).click(function(){
					initStudents(1,100000);
				});
				$("#student-choice-name",parent.parent.document).unbind("keyup");
				$("#student-choice-name",parent.parent.document).keyup(function(){
					this.value=this.value.replace(/^ +| +$/g,'');
				});
				document.onkeydown = function(e){ 
				    var ev = document.all ? window.event : e;
				    if(ev.keyCode==13) {
				    	initStudents(1,100000);
				    }
				};
				
			});
		});
	}
	function sendNotification(type){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var sendcode = $("#user-name",parent.parent.document).attr("code");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var receivecode = $("#anotices-students").attr("studentcodes");
		var content = $("#anotices-contents").text();
		var cmpptype = $("input[name='anotices-cmpptype']:checked").val();
		var idatetime = Math.round(strToDate(getSmpFormatDate(new Date(),true))/1000);
		var data = {
				cmd: "sendMessage",
				model: "teacher",
				userKey:userKey,
				data: {
					type:type,
					sendcode: sendcode,
					receivecode: receivecode,
					content: encodeURI(content),
					cmpptype: cmpptype,
					idatetime:idatetime,
					schoolcode:schoolcode
				},
				remark: "",
		};
		processBegin("正在发送中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("发送成功");
			}else{
				processError("发送失败");
			}
		},null,null);
	};
	function sendMessage(type,receivecode,content){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var sendcode = $("#user-name",parent.parent.document).attr("code");
		var idatetime = Math.round(strToDate(getSmpFormatDate(new Date(),true))/1000);
		var data = {
				cmd:"sendMessage",
				model:"teacher",
				userKey:userKey,
				data:{
					type:type,
					sendcode:sendcode,
					receivecode:receivecode,
					content:encodeURI(content),
					idatetime:idatetime
				},
				remark:"",
		};
		processBegin("正在发送中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("发送成功");
				
			}else{
				processError("发送失败");
			}
		},null,null);
	}
	function checkNews(){
		if($("#anews-contents").text() == ""){
			return false;        			
		}else{
			return true;
		}
	}
	function checkNotices(){
		if($("#anotices-contents").text() == ""){
			return false;        			
		}else{
			return true;
		}
	}
	function initTeachers(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"queryAllTeacher",
				model:"teacher",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "--";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				if(list.length > 0){
					for(var i = 0; i < list.length; i++){
						content += "<option value='"+ list[i].name +"' teachercode='" + list[i].teachercode +"' cellphone='" + list[i].cellphone + "' >" + list[i].name + "</option>";
					}
				}else{
					content += "--";
				}
			}else{
				content += "--";
			}
			$("#parent-cellphone").empty();
			$("#parent-cellphone").append(content);
		});
	}
	function initClasses(){
		var classes = $(".classes-choice",parent.document);
		var content = "";
		for(var i = 0; i < classes.length; i++){
			content += "<option value='"+classes[i].getAttribute("classcode")+"'>"+classes[i].innerHTML+"</option>";
		}
		$("#news-sendclass").empty();
		$("#news-sendclass").append(content);
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();
